<?php

namespace Addons\WeiSite\Controller;

use Addons\WeiSite\Controller\BaseController;

class WeiSiteController extends BaseController {
    function _initialize()
    {
        parent::_initialize();
        $UserInfo=M('UserInfo');
        $code=$_REQUEST['code'];
        if($_REQUEST['state']=='haifengjiaoy')
        {
            get_token('gh_f0e3527ec674');
        }
        //$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx65924fa1f1566b66&secret=c3b4cf2c72c8516a2c9f1736a5843006&code=".$code."&grant_type=authorization_code";
        //$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx3ed2ac9292ea64d8&secret=1940aba9d9cd96c90292a0d07d8bd5c6&code=".$code."&grant_type=authorization_code";


        $get_openid_url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxc0b5dfa06f872eee&secret=fbe5f95a747d6a719243741f7befeb19&code=".$code."&grant_type=authorization_code";


        //v('/*************get openid***************************/');
        $res=$res_openid=self::getCurl($get_openid_url);
        //v($res);


        //v('/*************get access_token***************************/');
        $get_access_token_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxc0b5dfa06f872eee&secret=fbe5f95a747d6a719243741f7befeb19";
        $res=self::getCurl($get_access_token_url);
        //v($res);


        //v('/*************get unionid***************************/');
        $get_unionid_url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$res['access_token']."&openid=".$res_openid['openid']."&lang=zh_CN";
        $res=self::getCurl($get_unionid_url);
        //p($res);
        $evn="test";
        //$evn="dev";//$new=1;
        //define('EVNL','test');
        define('EVNL','dev');
        if(!empty($res['unionid'])){
            $_SESSION['openid']=$res['unionid'];
            //$_SESSION['openid']=$res['openid'];
        }
        //unset($_SESSION);p($_SESSION);exit;
        //p($res);
        if(EVNL=='test')
        {
            if(!empty($new)&&$new==1)
            {
                $_SESSION['openid']='';
                get_token('gh_f0e3527ec674');
            }else{
                $_SESSION['openid']='oQmac';
                get_token('gh_f0e3527ec674');
            }


        }
        //p($_SESSION);
        //$_SESSION['openid']='erererere';
        /*if(empty($_SESSION['openid'])&&(_ACTION!='erweima'||_ACTION)){
            $this->redirect('/addon/WeiSite/WeiSite/erweima');exit;
        }*/
        /**/

    }
    static function getCurl($url,$data=null)
    {
        return self::curl($url,$data,'get');
    }
    static function postCurl($url,$data=null)
    {
        return self::curl($url,$data,'post');
    }
    static function curl($url,$data=null,$type)
    {
        if(empty($url)){
            exit("请输入URL");
        }
        $tmp_data='';
        if(!empty($args)){
            if(is_array($args)){
                foreach($args as $k=>$v){
                    $data.=$k.'='.$v.'&';
                }
            }elseif(is_string($args)){
                $data=$args;
            }
        }
        $curlObj = curl_init();
        curl_setopt( $curlObj, CURLOPT_URL, $url );
        curl_setopt( $curlObj, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $curlObj, CURLOPT_POST, (!empty($type)&&$type=='post')?1:0 );
        curl_setopt( $curlObj, CURLOPT_HTTPHEADER, [
            'Content-type:application/json;charset=UTF-8',
        ] );
        curl_setopt( $curlObj, CURLOPT_POSTFIELDS,$tmp_data );
        $res=json_decode(curl_exec( $curlObj ),true);
        return $res;
    }

    /**
     *
     *
     *
     *
     */
	function config() {
		// 使用提示
		$publicid = get_token_appinfo('','id');
		$normal_tips = '在微信里回复“微官网”即可以查看效果，也可点击:<a target="_blank" href="' . U ( 'preview' ) . '">预览</a>,<a id="copyLink" data-clipboard-text="' . U ( 'index',['publicid'=>$publicid]) . '">复制链接</a><script type="application/javascript">$.WeiPHP.initCopyBtn("copyLink");</script>';
		$this->assign ( 'normal_tips', $normal_tips );
		
		if (IS_POST) {
			$flag = D ( 'Common/AddonConfig' )->set ( _ADDONS, $_POST ['config'] );
			
			if ($flag !== false) {
				$this->success ( '保存成功', Cookie ( '__forward__' ) );
			} else {
				$this->error ( '保存失败' );
			}
			exit ();
		}
		
		parent::config ();
	}
	// 首页
	function index() {
		add_credit ( 'weisite', 86400 );
		
		if (file_exists ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/pigcms/Index_' . $this->config ['template_index'] . '.html' )) {
			$this->pigcms_index ();
			$this->display ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/pigcms/Index_' . $this->config ['template_index'] . '.html' );
		} else {
			$map ['token'] = get_token ();
			$map ['is_show'] = 1;
			$map ['pid'] = 0; // 获取一级分类
			                  
			// 幻灯片
			$slideshow = M ( 'weisite_slideshow' )->where ( $map )->order ( 'sort asc, id desc' )->select ();
			foreach ( $slideshow as &$vo ) {
				$vo ['img'] = get_cover_url ( $vo ['img'] );
			}
			$this->assign ( 'slideshow', $slideshow );
			// dump($slideshow);
			
			// 分类
			$category = M ( 'weisite_category' )->where ( $map )->order ( 'sort asc, id desc' )->select ();
			foreach ( $category as &$vo ) {
				$vo ['icon'] = get_cover_url ( $vo ['icon'] );
				empty ( $vo ['url'] ) && $vo ['url'] = addons_url ( 'WeiSite://WeiSite/lists', [
						'cate_id' => $vo ['id'] 
				] );
			}
			$this->assign ( 'category', $category );
			
			$map2 ['token'] = $map ['token'];
			$public_info = get_token_appinfo ( $map2 ['token'] );
			$this->assign ( 'publicid', $public_info ['id'] );
			
			$this->assign ( 'manager_id', $this->mid );
			$this->_footer ();
			$this->display ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/TemplateIndex/' . $this->config ['template_index'] . '/index.html' );
		}
	}
    function sendSMSCode()
    {
        $phone=$_POST['phone'];
        if(!preg_match("/^1[34578]\d{9}$/", $phone)){
            self::exitError('手机号格式不正确');
        }
        $str=range (0,9);
        shuffle($str);
        $str=array_slice($str,0,4);
        $new_sms_code=join('',$str);

        $send_result=sendSMSCode($phone,$new_sms_code);
        if($send_result){
            $_SESSION['sms_code_'.$phone][]=$new_sms_code;
            self::exitSuccess([],'验证码发送成功');
        }else{
            self::exitError('请稍后尝试');
        }
        //sendSMSCode();
    }
	// 分类列表
	function lists() {
        /*v(EVNL);
        p($_SESSION);*/
        if(empty($_SESSION['openid'])){
            $this->redirect('/addon/WeiSite/WeiSite/erweima');exit;
        }
        //echo U('addon/weisite/');
		$cate_id = I ( 'cate_id', 0, 'intval' );
		$subject = I ( 'subject', 0, 'intval' );
		empty ( $cate_id ) && $cate_id = I ( 'classid', 0, 'intval' );
		if (file_exists ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/pigcms/Index_' . $this->config ['template_lists'] . '.html' )) {
			$this->pigcms_lists ( $cate_id );
			$this->display ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/pigcms/Index_' . $this->config ['template_lists'] . '.html' );
		} else {
			//$map ['token'] = get_token ();
			$map ['token'] = 'gh_f0e3527ec674';
            $map['end_time']=['GT',time()];
			/*if ($cate_id) {
				$map ['cate_id'] = $cate_id;
			}*/
			/*$cate = M ( 'weisite_category' )->where ( 'id = ' . $map ['cate_id'] )->find ();
			$this->assign ( 'cate', $cate );
			// 二级分类
			$category = M ( 'weisite_category' )->where ( 'pid = ' . $map ['cate_id'] )->order ( 'sort asc, id desc' )->select ();*/
			if (! empty ( $category )) {
				foreach ( $category as &$vo ) {
					$vo ['icon'] = get_cover_url ( $vo ['icon'] );
					empty ( $vo ['url'] ) && $vo ['url'] = addons_url ( 'WeiSite://WeiSite/lists', [
							'cate_id' => $vo ['id'] 
					] );
				}
				$this->assign ( 'category', $category );
				$this->_footer ();
				$this->display ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/TemplateSubcate/' . $this->config ['template_subcate'] . '/index.html' );
			} else {

                    $page = I ( 'p', 1, 'intval' );
                    $row = isset ( $_REQUEST ['list_row'] ) ? intval ( $_REQUEST ['list_row'] ) : 20;

                    //$data = M ( 'custom_reply_news' )->where ( $map )->order ( 'sort asc, id DESC' )->page ( $page, $row )->select ();
                $subject_arr=['全部','升学','语文','数学','英语'];
                if(!empty($subject)){
                    $map['subject']=$subject;
                    $subject=$subject_arr[$subject];
                }else{
                    $subject=$subject_arr[0];
                }
                $this->assign('subject',$subject);
                    //$data = M ( 'custom_reply_news' )->where ( $map )->order ( 'sort asc, id DESC' )->page ( $page, $row )->select ();
                    $data = M ( 'custom_reply_news' )->where ( $map )->order ( 'sort asc, id DESC' )->select ();

                    /* 查询记录总数 */
                    //$count = M ( 'custom_reply_news' )->where ( $map )->count ();
                    $list_data ['list_data'] = $data;
                    // 分页
                    /*if ($count > $row) {
                        $page = new \Think\Page ( $count, $row );
                        $page->setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );
                        $list_data ['_page'] = $page->show ();
                    }*/
                    foreach($list_data['list_data'] as $k=>&$v)
                    {
                        $v['course_time']=date('m-d H:i',$v['start_time'])."~".date('m-d H:i',$v['end_time']);

                    }

                    //exit(json_encode($list_data));

                    $this->assign ( $list_data );
                    $openid=$_SESSION['openid'];
                    $map['user_openid']=$openid;
                    $my_data=M('UserCourse')->join('weip_custom_reply_news on weip_user_course.course_id=weip_custom_reply_news.id')->field('weip_custom_reply_news.*')->where($map)->order ( 'weip_user_course.ctime DESC' )->select();
                    foreach($my_data as $k=>&$v)
                    {
                        $v['course_time']=date('m-d H:i',$v['start_time'])."~".date('m-d H:i',$v['end_time']);

                    }
                    $this->assign('my_data',$my_data);
                    // dump ( $list_data );
                    $this->_footer ();
                //p(ONETHINK_ADDON_PATH . 'WeiSite/View/default/TemplateLists/' . $this->config ['template_lists'] . '/lists.html');
                    //$this->display ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/TemplateLists/' . $this->config ['template_lists'] . '/lists.html' );
                    $this->display ( "./Addons/WeiSite/View/default/TemplateLists/V4/lists.html" );


			}
		}
	}
    function erweima()
    {
        $this->display ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/TemplateDetail/' . $this->config ['template_detail'] . '/erweima.html' );
    }
    // 分类列表
    function lists_20160328() {

        $cate_id = I ( 'cate_id', 0, 'intval' );
        $subject = I ( 'subject', 0, 'intval' );
        empty ( $cate_id ) && $cate_id = I ( 'classid', 0, 'intval' );
        if (file_exists ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/pigcms/Index_' . $this->config ['template_lists'] . '.html' )) {
            $this->pigcms_lists ( $cate_id );
            $this->display ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/pigcms/Index_' . $this->config ['template_lists'] . '.html' );
        } else {
            $map ['token'] = get_token ();
            if ($cate_id) {
                $map ['cate_id'] = $cate_id;
            }
            $cate = M ( 'weisite_category' )->where ( 'id = ' . $map ['cate_id'] )->find ();
            $this->assign ( 'cate', $cate );
            // 二级分类
            $category = M ( 'weisite_category' )->where ( 'pid = ' . $map ['cate_id'] )->order ( 'sort asc, id desc' )->select ();
            if (! empty ( $category )) {
                foreach ( $category as &$vo ) {
                    $vo ['icon'] = get_cover_url ( $vo ['icon'] );
                    empty ( $vo ['url'] ) && $vo ['url'] = addons_url ( 'WeiSite://WeiSite/lists', [
                        'cate_id' => $vo ['id']
                    ] );
                }
                $this->assign ( 'category', $category );
                $this->_footer ();
                $this->display ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/TemplateSubcate/' . $this->config ['template_subcate'] . '/index.html' );
            } else {

                $page = I ( 'p', 1, 'intval' );
                $row = isset ( $_REQUEST ['list_row'] ) ? intval ( $_REQUEST ['list_row'] ) : 20;

                //$data = M ( 'custom_reply_news' )->where ( $map )->order ( 'sort asc, id DESC' )->page ( $page, $row )->select ();
                if(empty($_GET['d'])){
                    $this->display ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/TemplateLists/' . $this->config ['template_lists'] . '/lists.html' );
                    exit;
                }

                if(!empty($subject)){
                    $map['subject']=$subject;
                }
                $data = M ( 'custom_reply_news' )->where ( $map )->order ( 'sort asc, id DESC' )->page ( $page, $row )->select ();

                /* 查询记录总数 */
                $count = M ( 'custom_reply_news' )->where ( $map )->count ();
                $list_data ['list_data'] = $data;
                // 分页
                if ($count > $row) {
                    $page = new \Think\Page ( $count, $row );
                    $page->setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );
                    $list_data ['_page'] = $page->show ();
                }
                foreach($list_data['list_data'] as $k=>&$v)
                {
                    $v['course_time']=date('Y-m-d H:i',$v['start_time'])."~".date('H:i',$v['end_time']);

                }

                exit(json_encode($list_data));

                $this->assign ( $list_data );
                $openid=$_SESSION['openid'];
                $map['user_openid']=$openid;
                $my_data=M('UserCourse')->join('weip_custom_reply_news on weip_user_course.course_id=weip_custom_reply_news.id')->field('weip_custom_reply_news.*')->where($map)->order ( 'weip_user_course.ctime DESC' )->select();
                foreach($my_data as $k=>&$v)
                {
                    $v['course_time']=date('Y-m-d H:i',$v['start_time'])."~".date('H:i',$v['end_time']);

                }
                $this->assign('my_data',$my_data);
                // dump ( $list_data );
                $this->_footer ();
                $this->display ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/TemplateLists/' . $this->config ['template_lists'] . '/lists.html' );


            }
        }
    }
    // 分类列表
    function lists_bak() {
        $cate_id = I ( 'cate_id', 0, 'intval' );
        empty ( $cate_id ) && $cate_id = I ( 'classid', 0, 'intval' );
        if (file_exists ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/pigcms/Index_' . $this->config ['template_lists'] . '.html' )) {
            $this->pigcms_lists ( $cate_id );
            $this->display ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/pigcms/Index_' . $this->config ['template_lists'] . '.html' );
        } else {
            $map ['token'] = get_token ();
            if ($cate_id) {
                $map ['cate_id'] = $cate_id;
            }
            $cate = M ( 'weisite_category' )->where ( 'id = ' . $map ['cate_id'] )->find ();
            $this->assign ( 'cate', $cate );
            // 二级分类
            $category = M ( 'weisite_category' )->where ( 'pid = ' . $map ['cate_id'] )->order ( 'sort asc, id desc' )->select ();
            if (! empty ( $category )) {
                foreach ( $category as &$vo ) {
                    $vo ['icon'] = get_cover_url ( $vo ['icon'] );
                    empty ( $vo ['url'] ) && $vo ['url'] = addons_url ( 'WeiSite://WeiSite/lists', [
                        'cate_id' => $vo ['id']
                    ] );
                }
                $this->assign ( 'category', $category );
                $this->_footer ();
                $this->display ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/TemplateSubcate/' . $this->config ['template_subcate'] . '/index.html' );
            } else {

                $page = I ( 'p', 1, 'intval' );
                $row = isset ( $_REQUEST ['list_row'] ) ? intval ( $_REQUEST ['list_row'] ) : 20;

                //$data = M ( 'custom_reply_news' )->where ( $map )->order ( 'sort asc, id DESC' )->page ( $page, $row )->select ();
                $data = M ( 'custom_reply_news' )->where ( $map )->order ( 'sort asc, id DESC' )->page ( $page, $row )->select ();
                /* 查询记录总数 */
                $count = M ( 'custom_reply_news' )->where ( $map )->count ();
                $list_data ['list_data'] = $data;
                // 分页
                if ($count > $row) {
                    $page = new \Think\Page ( $count, $row );
                    $page->setConfig ( 'theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%' );
                    $list_data ['_page'] = $page->show ();
                }
                foreach($list_data['list_data'] as $k=>&$v)
                {
                    $v['course_time']=date('Y-m-d H:i',$v['start_time'])."~".date('H:i',$v['end_time']);

                }
                $this->assign ( $list_data );
                // dump ( $list_data );
                //p($list_data);
                $this->_footer ();
                $this->display ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/TemplateLists/' . $this->config ['template_lists'] . '/lists.html' );
            }
        }
    }
	// 详情
	function detail() {
		if (file_exists ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/pigcms/Index_' . $this->config ['template_detail'] . '.html' )) {
			$this->pigcms_detail ();
			$this->display ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/pigcms/Index_' . $this->config ['template_detail'] . '.html' );
		} else {
			$map ['id'] = I ( 'get.id', 0, 'intval' );
			$info = M ( 'custom_reply_news' )->where ( $map )->find ();
            if(!empty($info['start_time'])){
                $info['start_time']=date('Y-m-d H:i',$info['start_time']);
            }
            if(!empty($info['end_time'])){
                $info['end_time']=date('H:i',$info['end_time']);
            }
            if(!empty($info['cover'])){
                $info['cover']=get_cover_url($info['cover']);
            }
            M ( 'custom_reply_news' )->where ( $map )->setInc ( 'view_count' );
            $is_visit=M('UserCourse')->where(['user_openid'=>$_SESSION['openid'],'course_id'=>$map['id']])->find();
            if($is_visit){
                $info['is_visited']=true;
            }else{
                $info['is_visited']=false;
            }
            exit(json_encode($info));
			$this->assign ( 'info', $info );
			M ( 'custom_reply_news' )->where ( $map )->setInc ( 'view_count' );
			$is_visit=M('UserCourse')->where(['user_openid'=>$_SESSION['openid'],'course_id'=>$map['id']])->find();

            p($is_visit);
			$this->_footer ();
			$this->display ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/TemplateDetail/' . $this->config ['template_detail'] . '/detail.html' );
		}
	}

    // 详情
    function shipin() {
        $is_register=M('UserInfo')->where(['openid'=>$_SESSION['openid']])->find();
        if(empty($is_register))
        {
            $this->redirect('/addon/WeiSite/WeiSite/erweima');exit;
        }
        $room=I('room','0','intval');

        //exit("shipin page:roomid=$room");
        $this->assign('roomid',$room);
        //$this->display ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/TemplateDetail/' . $this->config ['template_detail'] . '/shipin.html' );
        $this->display ( "./Addons/WeiSite/View/default/TemplateDetail/V4/shipin.html" );
        exit;
        if (file_exists ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/pigcms/Index_' . $this->config ['template_detail'] . '.html' )) {
            $this->pigcms_detail ();
            $this->display ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/pigcms/Index_' . $this->config ['template_detail'] . '.html' );
        } else {
            $map ['id'] = I ( 'get.id', 0, 'intval' );
            $info = M ( 'custom_reply_news' )->where ( $map )->find ();
            $this->assign ( 'info', $info );

            M ( 'custom_reply_news' )->where ( $map )->setInc ( 'view_count' );

            $this->_footer ();
            //p(ONETHINK_ADDON_PATH . 'WeiSite/View/default/TemplateDetail/' . $this->config ['template_detail'] . '/detail.html');
            $this->display ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/TemplateDetail/' . $this->config ['template_detail'] . '/detail.html' );
        }
    }
    function isRegistered()
    {
        $UserInfo=M('UserInfo');
        $openid=$_SESSION['openid'];

        $user_info_data=$UserInfo->where(['openid'=>$openid])->find();
        if(empty($user_info_data)){
            return false;
        }
        return ['user_id'=>$user_info_data['id'],'user_openid'=>$user_info_data['openid']];
    }
    function exitIsRegistered()
    {

        $info=$this->isRegistered();
        if($info){
            $info['isok']=true;
        }else{
            $info['isok']=false;
        }
        exit(json_encode($info));

    }
	function baoming()
    {

        $UserInfo=M('UserInfo');
        $UserInfo->startTrans();
        $register_info=$this->isRegistered();
        //v('1');
        //v($register_info);
        if(!$register_info)
        {
            $register_info=$this->register();
            //v('2');
            //v($register_info);
        }
        //v('3');
        //v($register_info);v($_POST['course_id']);
        if(!empty($register_info['user_id'])&&!empty($_POST['course_id']))
        {
            //v('4');
            $sign_up_data=$this->signUp($register_info['user_id'],$register_info['user_openid'],$_POST['course_id']);
            //v('5');
            //v($sign_up_data);
            if(!$sign_up_data['isok']){
                self::exitError($sign_up_data['msg']);
            }
        }else{
            self::exitError('信息有误');
        }

        if($sign_up_data['isok']){
            $UserInfo->commit();
            self::exitSuccess('报名成功');
        }else{
            $UserInfo->rollback();
        }
    }
    function register()
    {
        $openid=$_SESSION['openid'];
        $data=[];
        if(empty($openid)){
            self::exitError('系统超时,请重新进入');
        }
        $data['openid']=$openid;
        $data['name']=$username=I('name','','htmlspecialchars');
        if(empty($username)){
            self::exitError('名字不能为空');
        }
        $data['phone']=$phone=$_POST['phone'];
        if(!preg_match("/^1[34578]\d{9}$/", $phone)){
            self::exitError('手机号格式不正确');
        }
        $smscode=I('smscode','0','intval');
        if(empty($smscode)){
            self::exitError('手机验证码不正确');
        }
        $mobile_sms_code=$_SESSION['sms_code_'.$phone];
        $tag=false;
        if(!empty($mobile_sms_code)){
            foreach($mobile_sms_code as $k=>$v)
            {
                if($smscode==$v){
                    $tag=true;
                }
            }
        }
        if(!$tag){
            self::exitError('验证码错误');
        }
        $data['invitecode']=$invitecode=I('invitecode');
        $data['ctime']=date('Y-m-d H:i:s');
        $UserInfo=M('UserInfo');
        $user_info_data=$UserInfo->where(['openid'=>$openid])->find();
        if(!$user_info_data){
            $UserInfo->create();
            if($id=$UserInfo->data($data)->add()){

                $_SESSION['is_register']=true;
                $user_info_data['user_id']=$id;
                $user_info_data['user_openid']=$openid;
            }else{
                self::exitError('用户注册失败');
            }

        }else{
            $user_info_data['user_openid']=$user_info_data['openid'];
            if($user_info_data['id']){
                $user_info_data['user_id']=$user_info_data['id'];
            }
        }
        return $user_info_data;
    }
    function signUp($user_id,$user_openid,$course_id)
    {
        $UserCourse=M('UserCourse');
        $user_course_data=$UserCourse->where(['user_openid'=>$user_openid,'course_id'=>$course_id])->find();
        if(empty($user_course_data)){
            $isok=$UserCourse->add(['user_id'=>$user_id,'user_openid'=>$user_openid,'course_id'=>$course_id,'ctime'=>date('Y-m-d H:i:s')]);
            M ( 'custom_reply_news' )->where ( ['id'=>$course_id] )->setInc ( 'visit_count' );
            if($isok)return ['isok'=>true,'msg'=>'报名成功'];
            return ['isok'=>false,'msg'=>'报名失败,请稍后尝试'];
        }
        return ['isok'=>true,'msg'=>'您已经报名过,请直接点击观看'];

    }
    static function exitError($msg=null,$data=null){
        exit(json_encode(['isok'=>false,'msg'=>$msg,'data'=>$data],JSON_UNESCAPED_UNICODE));
    }
    static function exitSuccess($data=null,$msg=''){
        exit(json_encode(['isok'=>true,'msg'=>$msg,'data'=>$data],JSON_UNESCAPED_UNICODE));
    }
	// 3G页面底部导航
	function _footer($temp_type = 'weiphp') {
		if ($temp_type == 'pigcms') {
			$param ['token'] = $token = get_token ();
			$param ['temp'] = $this->config ['template_footer'];
			$url = U ( 'Home/Index/getFooterHtml', $param );
			$html = wp_file_get_contents ( $url );
			// dump ( $url );
			// dump ( $html );
			$file = RUNTIME_PATH . $token . '_' . $this->config ['template_footer'] . '.html';
			if (! file_exists ( $file ) || true) {
				file_put_contents ( $file, $html );
			}
			
			$this->assign ( 'cateMenuFileName', $file );
		} else {
			$list = D ( 'Addons://WeiSite/Footer' )->get_list ();
			
			foreach ( $list as $k => $vo ) {
				if ($vo ['pid'] != 0)
					continue;
				
				$one_arr [$vo ['id']] = $vo;
				unset ( $list [$k] );
			}
			
			foreach ( $one_arr as &$p ) {
				$two_arr = [];
				foreach ( $list as $key => $l ) {
					if ($l ['pid'] != $p ['id'])
						continue;
					
					$two_arr [] = $l;
					unset ( $list [$key] );
				}
				
				$p ['child'] = $two_arr;
			}
			$this->assign ( 'footer', $one_arr );
			$html = $this->fetch ( ONETHINK_ADDON_PATH . 'WeiSite/View/default/TemplateFooter/' . $this->config ['template_footer'] . '/footer.html' );
			$this->assign ( 'footer_html', $html );
		}
	}
	function _deal_footer_data($vo, $k) {
		$arr = [
				'id' => $vo ['id'],
				'fid' => $vo ['pid'],
				'token' => $vo ['token'],
				'name' => $vo ['title'],
				'orderss' => 0,
				'picurl' => get_cover_url ( $vo ['icon'] ),
				'url' => $vo ['url'],
				'status' => "1",
				'RadioGroup1' => "0",
				'vo' => [],
				'k' => $k 
		];
		return $arr;
	}
	function coming_soom() {
		$this->display ();
	}
	function tvs1_video() {
		$this->display ();
	}
	
	/*
	 * 兼容小猪CMS模板
	 *
	 * 移植方法：
	 * 1、把 tpl\static\tpl 目录下的所有文档复制到 weiphp的 Addons\WeiSite\View\default\pigcms 目录下
	 * 2、把 tpl\Wap\default 目录下的所有文档也复制到 weiphp的 Addons\WeiSite\View\default\pigcms 目录下
	 * 3、把 tpl\User\default\common\ 目录下的所有图片文件复制到 weiphp的 Addons\WeiSite\View\default\pigcms 目录下
	 * 4、把 PigCms\Lib\ORG\index.Tpl.php 文件复制到 weiphp的 Addons\WeiSite\View\default\pigcms 目录下
	 * 5、把pigcms 目录下所有文档代码里的 Wap/Index/lists 替换成 Home/Addons/execute?_addons=WeiSite&_controller=WeiSite&_action=lists
	 * 6、把pigcms 目录下所有文档代码里的 Wap/Index/index 替换成 Home/Addons/execute?_addons=WeiSite&_controller=WeiSite&_action=index
	 */
	function pigcms_init() {
		// dump ( 'pigcms_init' );
		C ( 'TMPL_L_DELIM', '{pigcms:' );
		// C ( 'TMPL_FILE_DEPR', '_' );
		
		define ( 'RES', ONETHINK_ADDON_PATH . 'WeiSite/View/default/pigcms/common' );
		
		$public_info = get_token_appinfo ();
		$manager = get_userinfo ( $public_info ['uid'] );
		
		// 站点配置
		$data ['f_logo'] = get_cover_url ( C ( 'SYSTEM_LOGO' ) );
		$data ['f_siteName'] = C ( 'WEB_SITE_TITLE' );
		$data ['f_siteTitle'] = C ( 'WEB_SITE_TITLE' );
		$data ['f_metaKeyword'] = C ( 'WEB_SITE_KEYWORD' );
		$data ['f_metaDes'] = C ( 'WEB_SITE_DESCRIPTION' );
		$data ['f_siteUrl'] = SITE_URL;
		$data ['f_qq'] = '';
		$data ['f_qrcode'] = '';
		$data ['f_ipc'] = C ( 'WEB_SITE_ICP' );
		$data ['reg_validDays'] = 30;
		
		// 用户信息
		$data ['user'] = [
				'id' => $GLOBALS ['myinfo'] ['uid'],
				'openid' => get_openid (),
				'username' => $GLOBALS ['myinfo'] ['nickname'],
				'mp' => $public_info ['token'],
				'password' => $GLOBALS ['myinfo'] ['password'],
				'email' => $GLOBALS ['myinfo'] ['email'],
				'createtime' => $GLOBALS ['myinfo'] ['reg_time'],
				'lasttime' => $GLOBALS ['myinfo'] ['last_login_time'],
				'status' => 1,
				'createip' => $GLOBALS ['myinfo'] ['reg_ip'],
				'lastip' => $GLOBALS ['myinfo'] ['last_login_ip'],
				'smscount' => 0,
				'inviter' => 1,
				'gid' => 5,
				'diynum' => 0,
				'activitynum' => 0,
				'card_num' => 0,
				'card_create_status' => 0,
				'money' => 0,
				'moneybalance' => 0,
				'spend' => 0,
				'viptime' => $GLOBALS ['myinfo'] ['last_login_time'] + 86400,
				'connectnum' => 0,
				'lastloginmonth' => 0,
				'attachmentsize' => 0,
				'wechat_card_num' => 0,
				'serviceUserNum' => 0,
				'invitecode' => '',
				'remark' => '' 
		];
		
		// 微网站配置信息
		$data ['homeInfo'] = [
				'id' => $manager ['uid'],
				'token' => $public_info ['token'],
				'title' => $this->config ['title'],
				'picurl' => get_cover_url ( $this->config ['cover'] ),
				// 'apiurl' => "",
				// 'homeurl' => "",
				'info' => $this->config ['info'],
				// 'musicurl' => "",
				// 'plugmenucolor' => "#5CFF8D",
				'copyright' => $manager ['copy_right'],
				// 'radiogroup' => "12",
				// 'advancetpl' => "0"
				'logo' => get_cover_url ( $this->config ['cover'] ) 
		];
		
		// 背景图
		$data ['flashbgcount'] = 1;
		$data ['flashbg'] [0] = [
				'id' => $this->config ['background_id'],
				'token' => $public_info ['token'],
				'img' => $this->config ['background'],
				'url' => "javascript:void(0)",
				'info' => "背景图片",
				'tip' => '2' 
		];
		$data ['flashbgcount'] = count ( $data ['flashbg'] );
		
		// 幻灯片
		$slideshow = M ( 'weisite_slideshow' )->where ( $map )->order ( 'sort asc, id desc' )->select ();
		foreach ( $slideshow as $vo ) {
			$data ['flash'] [] = [
					'id' => $vo ['id'],
					'token' => $vo ['token'],
					'img' => get_cover_url ( $vo ['img'] ),
					'url' => $vo ['url'],
					'info' => $vo ['title'],
					'tip' => '1' 
			];
		}
		$data ['num'] = count ( $data ['flash'] );
		
		// 底部栏
		$this->_footer ( 'pigcms' );
		
		// 设置版权信息
		$data ["iscopyright"] = 0;
		$data ["copyright"] = $data ["siteCopyright"] = empty ( $manager ['copy_right'] ) ? C ( 'COPYRIGHT' ) : $manager ['copy_right'];
		// 分享
		$data ['shareScript'] = '';
		
		$data ['token'] = $public_info ['token'];
		$data ['wecha_id'] = $public_info ['wechat'];
		
		$this->assign ( $data );
		
		// 模板信息
		if (file_exists ( ONETHINK_ADDON_PATH . _ADDONS . '/View/default/pigcms/index.Tpl.php' )) {
			$pigcms_temps = require_once ONETHINK_ADDON_PATH . _ADDONS . '/View/default/pigcms/index.Tpl.php';
			foreach ( $pigcms_temps as $k => $vo ) {
				$temps [$vo ['tpltypename']] = $vo;
			}
		}
		
		if (file_exists ( ONETHINK_ADDON_PATH . _ADDONS . '/View/default/pigcms/cont.Tpl.php' )) {
			$pigcms_temps = require_once ONETHINK_ADDON_PATH . _ADDONS . '/View/default/pigcms/cont.Tpl.php';
			foreach ( $pigcms_temps as $k => $vo ) {
				$temps [$vo ['tpltypename']] = $vo;
			}
		}
		$tpl = [
				'id' => $public_info ['id'],
				'routerid' => "",
				'uid' => $public_info ['uid'],
				'wxname' => $public_info ['public_name'],
				'winxintype' => $public_info ['type'],
				'appid' => $public_info ['appid'],
				'appsecret' => $public_info ['secret'],
				'wxid' => $public_info ['id'],
				'weixin' => $public_info ['wechat'],
				'headerpic' => get_cover_url ( $GLOBALS ['myinfo'] ['headface_url'] ),
				'token' => $public_info ['token'],
				'pigsecret' => $public_info ['token'],
				'province' => $GLOBALS ['myinfo'] ['province'],
				'city' => $GLOBALS ['myinfo'] ['city'],
				'qq' => $GLOBALS ['myinfo'] ['qq'],
				// 'wxfans' => "0",
				// 'typeid' => "8",
				// 'typename' => "服务",
				// 'tongji' => "",
				// 'allcardnum' => "0",
				// 'cardisok' => "0",
				// 'yetcardnum' => "0",
				// 'totalcardnum' => "0",
				// 'createtime' => "1440150418",
				// 'updatetime' => "1440150418",
				// 'transfer_customer_service' => "0",
				// 'openphotoprint' => "0",
				// 'freephotocount' => "3",
				// 'oauth' => "0",
				'color_id' => 0,
				
				'tpltypeid' => $temps [$this->config ['template_index']] ['tpltypeid'],
				'tpltypename' => $this->config ['template_index'],
				
				'tpllistid' => $temps [$this->config ['template_lists']] ['tpltypeid'],
				'tpllistname' => $this->config ['template_lists'],
				
				'tplcontentid' => $temps [$this->config ['template_detail']] ['tpltypeid'],
				'tplcontentname' => $this->config ['template_detail'] 
		];
		$this->assign ( 'tpl', $tpl );
		$this->assign ( 'wxuser', $tpl );
	}
	function pigcms_index() {
		$this->pigcms_init ();
		
		$cate = $this->_pigcms_cate ( 0 );
		$this->assign ( 'info', $cate );
	}
	function pigcms_lists($cate_id) {
		$this->pigcms_init ();
		
		$map ['token'] = get_token ();
		$cateArr = M ( 'weisite_category' )->where ( $map )->getField ( 'id,title' );
		
		$thisClassInfo = [];
		if ($cate_id) {
			$map ['cate_id'] = $cate_id;
			
			$thisClassInfo = $this->_deal_cate ( $cateArr [$cate_id] );
		}
		
		$data = M ( 'custom_reply_news' )->where ( $map )->order ( 'sort asc, id DESC' )->select ();
		foreach ( $data as $vo ) {
			$info [] = [
					'id' => $vo ['id'],
					'uid' => 0,
					'uname' => $vo ['author'],
					'keyword' => $vo ['keyword'],
					'type' => 2,
					'text' => $vo ['intro'],
					'classid' => $vo ['cate_id'],
					'classname' => $vo [''],
					'pic' => get_cover_url ( $vo ['cover'] ),
					'showpic' => 1,
					'info' => strip_tags ( htmlspecialchars_decode ( mb_substr ( $vo ['content'], 0, 10, 'utf-8' ) ) ),
					'url' => $this->_getNewsUrl ( $vo ),
					'createtime' => $vo ['cTime'],
					'uptatetime' => $vo ['cTime'],
					'click' => $vo ['view_count'],
					'token' => $vo ['token'],
					'title' => $vo ['title'],
					'usort' => $vo ['sort'],
					'name' => $vo ['title'],
					'img' => get_cover_url ( $vo ['cover'] ) 
			];
		}
		
		$this->assign ( 'info', $info );
		$this->assign ( 'thisClassInfo', $thisClassInfo );
	}
	function pigcms_detail() {
		$this->pigcms_init ();
		
		$cate = $this->_pigcms_cate ( 0 );
		$this->assign ( 'info', $cate );
		
		$map ['id'] = I ( 'get.id', 0, 'intval' );
		$res = M ( 'custom_reply_news' )->where ( $map )->find ();
		$res = $this->_deal_news ( $res, 1 );
		$this->assign ( 'res', $res );
		M ( 'custom_reply_news' )->where ( $map )->setInc ( 'view_count' );
		
		$map2 ['cate_id'] = $res ['cate_id'];
		$map2 ['id'] = [
				'exp',
				'!=' . $map ['id'] 
		];
		$lists = M ( 'custom_reply_news' )->where ( $map2 )->order ( 'id desc' )->limit ( 5 )->select ();
		foreach ( $lists as &$new ) {
			$new = $this->_deal_news ( $new );
		}
		
		$this->assign ( 'lists', $lists );
	}
	function _pigcms_cate($pid = null) {
		$map ['token'] = get_token ();
		$map ['is_show'] = 1;
		$pid === null || $map ['pid'] = $pid; // 获取一级分类
		
		$category = M ( 'weisite_category' )->where ( $map )->order ( 'sort asc, id desc' )->select ();
		$count = count ( $category );
		foreach ( $category as $k => $vo ) {
			$param ['cate_id'] = $vo ['id'];
			$url = empty ( $vo ['url'] ) ? $vo ['url'] = addons_url ( 'WeiSite://WeiSite/lists', $param ) : $vo ['url'];
			$pid = intval ( $vo ['pid'] );
			$res [$pid] [$vo ['id']] = $this->_deal_cate ( $vo, $count - $k );
		}
		
		foreach ( $res [0] as $vv ) {
			if (! empty ( $res [$vv ['id']] )) {
				$vv ['sub'] = $res [$vv ['id']];
				unset ( $res [$vv ['id']] );
			}
		}
		
		return $res [0];
	}
	function _deal_cate($vo, $key = 1) {
		return [
				'id' => $vo ['id'],
				'fid' => $vo ['pid'],
				'name' => $vo ['title'],
				'info' => $vo ['title'],
				'sorts' => $vo ['sort'],
				'img' => get_cover_url ( $vo ['icon'] ),
				'url' => $url,
				'status' => 1,
				'path' => empty ( $vo ['pid'] ) ? 0 : '0-' . $vo ['pid'],
				'tpid' => 1,
				'conttpid' => 1,
				'sub' => [],
				'key' => $key,
				'token' => $vo ['token'] 
		];
	}
	function _deal_news($vo, $type = 0) {
		$map ['id'] = $vo ['cate_id'];
		return [
				'id' => $vo ['id'],
				'uid' => 0,
				'uname' => $vo ['author'],
				'keyword' => $vo ['keyword'],
				'type' => 2,
				'text' => $vo ['intro'],
				'classid' => $vo ['cate_id'],
				'classname' => empty ( $vo ['cate_id'] ) ? '' : M ( 'weisite_category' )->where ( $map )->getField ( 'title' ),
				'pic' => get_cover_url ( $vo ['cover'] ),
				'showpic' => 1,
				'info' => $type == 0 ? strip_tags ( htmlspecialchars_decode ( mb_substr ( $vo ['content'], 0, 10, 'utf-8' ) ) ) : $vo ['content'],
				'url' => $this->_getNewsUrl ( $vo ),
				'createtime' => $vo ['cTime'],
				'uptatetime' => $vo ['cTime'],
				'click' => $vo ['view_count'],
				'token' => $vo ['token'],
				'title' => $vo ['title'],
				'usort' => $vo ['sort'],
				'name' => $vo ['title'],
				'img' => get_cover_url ( $vo ['cover'] ) 
		];
	}
	function _getNewsUrl($info) {
		$param ['token'] = get_token ();
		$param ['openid'] = get_openid ();
		
		if (! empty ( $info ['jump_url'] )) {
			$url = replace_url ( $info ['jump_url'] );
		} else {
			$param ['id'] = $info ['id'];
			$url = U ( 'detail', $param );
		}
		return $url;
	}
	/* 预览 */
	function preview(){
		$publicid = get_token_appinfo('','id');
	    $url = addons_url('WeiSite://WeiSite/index',['publicid'=>$publicid]);
	    $this->assign('url', $url);
	    $this->display(SITE_PATH . '/Application/Home/View/default/Addons/preview.html');
	}
}
